
//补零函数
function zero(n) {
    return n >= 10 ? n : '0' + n
}

//加载财务资金趋势
function loadCWZJQS(data) {
	var dataYmb = {
		    "xdataArr": data.xdataArr,
		             "legend": {
		                 "data": [
		                     "在岸人民币(CNY)",
		                     "离岸人民币(CNH)"
		                 ]
		             },
		             "series": [
		                 {
		                     "name": "在岸人民币(CNY)",
		                     "type": "line",
		                     "data": data.cnyList,
		                 },
		                 {
		                     "name": "离岸人民币(CNH)",
		                     "type": "line",
		                     "data": data.cnhList,
		                 }
		             ]
		         };
	var dataYb = {
		"xdataArr":data.xdataArr,
		"legend": {
			"data": [
				"美元(USD)",
				"欧元(EUR)",
				"英镑(GBP)",
				"其他",
			]
		},
		"series": [
			{
				"name": "美元(USD)",
				"type": "line",
				"data": data.usdList,
			},
			{
				"name": "欧元(EUR)",
				"type": "line",
				"data": data.eurList,
			},
			{
				"name": "英镑(GBP)",
				"type": "line",
				"data": data.gbpList,
			}
		]
	};
	initEcharts('id_echarts', dataYmb);
	initEcharts('id_echarts1', dataYb);

	//刷新时间
	$('#updateTime1').text(data.updateTime1);
	$('#updateTime2').text(data.updateTime1);
}


/**
 */
function initEcharts(id, data) {
	// 处理yaxisIndex
	for (var i = 0; i < data.series.length; i++) {
		data.series[i]['yAxisIndex'] = data.series[i]['yaxisIndex'];
	}
	
	const colors = ['#5470C6', '#91CC75', '#EE6666'];
	var option = {
	  color: colors,
	  tooltip: {
	    trigger: 'axis',
	    axisPointer: {
	      type: 'cross'
	    }
	  },
	  grid: {
			left: '2%',
			right: '2%',
			bottom: '2%',
		    containLabel: true
	  },
	  toolbox: {
	    feature: {
	      dataView: { show: false, readOnly: false },
	      restore: { show: false },
	      saveAsImage: { show: false }
	    }
	  },
	  legend: data.legend,
	  /*
	  {
	    data: ['销售金额', '右边Y轴曲线图']
	  },
	  */
	  xAxis: [
	    {
	      type: 'category',
	      axisTick: {
	        alignWithLabel: true
	      },
	      // prettier-ignore
	      data: data.xdataArr // X轴数组
	      //data: ['类别1', '类别2', '类别3', '类别4']
	    }
	  ],
	  yAxis: [
	    {
	      type: 'value',
	      name: '金额',
	      position: 'left',
	      alignTicks: true,
	      axisLine: {
	        show: true,
	        lineStyle: {
	          color: colors[0]
	        }
	      },
	      axisLabel: {
	        formatter: '{value}'
	      }
	    }

	  ],
	  series: data.series
	  /*
	  [
	    {
	      name: '销售金额',
	      type: 'line',
	      data: [2.0, 4.9, 7.0, 23.2, 23.2, 23.2, 23.2]
	    },
	    {
	      name: '右边Y轴曲线图',
	      type: 'line',
	      yAxisIndex: 1,
	      data: [6.9, 5.9, 9.0, 26.4, 26.4, 26.4, 26.4]
	    }
	  ]
	  */
	};
	echarts.init(document.getElementById(id)).setOption(option, true);
}

// 加载所有
function loadAll(){
	$.ajax({
		type: "POST",
		url: '/web/bi/finance/getBiFinancialTrend',
		async: true,
		success: function (data) {
			if (data.code == 0) {
				loadCWZJQS(data); // 财务资金趋势
			} else {
				alert(data.msg);
			}
		}, error: function() {
			alert("操作失败，请稍后再试！", { icon: 2 });
		}
	});
}

$(document).ready(function(){
	loadAll();
});